var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tc: 0,
    minHeight: 0,
    minWidth: 0,
    yu_e: 0,
    tiqu: 0,
    uid: 0,
    token: '',
    canMaxTiXian: 0,
    modalHidden: true,

    colorSet:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var tc = app.get_tc();
    var titlename = tc == 1 ? '余額提現' : '余额提现';
    wx.setNavigationBarTitle({
      title: titlename,
    })
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          minHeight: res.windowHeight,
          minWidth:res.windowWidth,
          tc: app.get_tc()
        });
      },
    });

    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color
      })
    });

    app.LoginUserInfo(function (res) {
      if (res && res != -1) {
      that.setData({
        uid: res.uid,
        token: res.token,
      });
      }
    });

    this.getYu_e();
    this.getTixian();
  },

  all_tiqu: function() {
    var that = this;
    that.setData({
      tiqu: that.data.canMaxTiXian
    });
  },

  formSubmit: function(e) {
    app.saveFormId(e.detail.formId);
    var that = this;
    console.log("formId ", e.detail.formId);
    var tx_coin = e.detail.value.tiqu_input;
    if (tx_coin < 1) {
      var app1 = that.data.tc == 1 ? '請提現大於等於1元' : '请提现大于等于1元';
      app.dialog(app1, '提示');
    } else if (tx_coin > that.data.canMaxTiXian) {
      var app2 = that.data.tc == 1 ? '可提現余額不足' : '可提现余额不足';
      app.dialog(app2, '提示');
    } else {
      var data = {
        // ac: "extractcoin",
        // amount: tx_coin * 100,
        ac: "wxsendhongbao",
        total_fee: tx_coin * 100,
        uid: that.data.uid,
        "privatekey": utilMd5.hexMD5(that.data.uid + 'wxsendhongbao' + that.data.token)
      };
      console.log("wxsendhongbao: ", data);
      util.api({
        method: "POST",
        data: data,
        success: function (responseData) {
          var result = responseData.data;
          console.log("wxsendhongbao result ", result);
          if (result.res) {
            that.setData({
              modalHidden:false
            })
          } else {
            app.dialog(result.msg, '提示');
          }
        }
      });
    }
  },

  getYu_e: function () {
    var that = this;
    util.api({
      method: "POST",
      data: {
        ac: "coininfo",
        uid: that.data.uid,
        "privatekey": utilMd5.hexMD5(that.data.uid + 'coininfo' + that.data.token)
      },
      success: function (responseData) {
        var result = responseData.data;
        console.log("yu_e result ", result);
        if (result && result.coin) {
          that.setData({
            yu_e: (result.coin / 100.0).toFixed(2) //保留两位小数
          });
        }
      }
    });
  },

  //获取可提现金额
  getTixian: function () {
    var that = this;
    util.api({
      method: "GET",
      data: {
        ac: "gettixian",
        uid: that.data.uid,
        publickey: util.publiccode
      },
      success: function (responseData) {
        var result = responseData.data;
        console.log("gettixian result ", result);
        if (result && result.tixian) {
          that.setData({
            canMaxTiXian: (result.tixian / 100.0).toFixed(2) //保留两位小数
          });
        }
      }
    });
  },

  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.navigateTo({ url: url })
  },

  modalBindaconfirm: function(){
    this.setData({
      modalHidden: true
    });
    wx.navigateBack({//返回上一页
      delta: 1
    });
  }
})